﻿package 
{
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextFieldAutoSize;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.special.DAEMC;
	import org.papervision3d.view.BasicView;
	import flash.ui.Keyboard;
	import flash.text.TextField;
	
	public class Main extends BasicView 
	{
		private var model:DAEMC;
		private var _isWalking:Boolean;
		private var _rotationDir:Number;
		private var tf:TextField;
		
		public function Main():void
		{
			//stage.scaleMode = StageScaleMode.SHOW_ALL;
				model = new DAEMC(true, null, 0);
				model.load("modelo.DAE", new MaterialsList( {  scout_red:new BitmapFileMaterial("scout_flat.jpg", true) } ));
				model.scale = 3;
				
				model.setFrameLabel(0, "idle");
				model.setFrameLabel(11, "startWalk");
				model.setFrameLabel(17, "loopWalk");
				
				model.addFrameScript(10, loopIDLE);
				model.addFrameScript(40, loopWALK);
				
				scene.addChild(model);
				
			var floor:Plane = new Plane()
				floor.rotationX = 90;
				scene.addChild(floor);
				
			camera.y = 300;
			camera.target.y = 300;
			
			
			////////////////////////////
			_isWalking = false;
			_rotationDir = 0;
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, lKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, lKeyDown);
			
			
			startRendering();
			
			tf = new TextField()
				tf.autoSize = TextFieldAutoSize.LEFT;
				tf.htmlText = 'Video tutorial by: <a href="http://pablobandin.wordpress.com" target="_blank"><u><font color="#0000ff">Pablo Bandin .wordpress.com</font></u></a> - 12/December/2008';
				addChild(tf)
				
				var tmpH:Number = tf.height;
				
			tf = new TextField()
				tf.autoSize = TextFieldAutoSize.LEFT;
				tf.multiline = true;
				addChild(tf)
				tf.y = tmpH + 10;
			
				
			
		}
		
		private function lKeyDown(e:KeyboardEvent):void {
			switch(e.keyCode)
			{
				case Keyboard.UP:
					
				var dummy:Boolean =  e.type == KeyboardEvent.KEY_DOWN;
				
					if (dummy)
					{
						if (!_isWalking){
							model.gotoAndPlay("startWalk");
						}
					}
					else {
						model.gotoAndPlay("idle")
					}
						
					
					_isWalking = dummy;
					
					break;
					
				case Keyboard.LEFT:
				case Keyboard.RIGHT:
					if(e.type == KeyboardEvent.KEY_DOWN)
						_rotationDir = e.keyCode == Keyboard.LEFT? -5:5;
					else
						_rotationDir = 0;
					break;
			}
		}
		
		private function loopWALK(mod:DAEMC):void{
			model.gotoAndPlay("loopWalk");
		}
		
		private function loopIDLE(mod:DAEMC):void{
			model.gotoAndPlay("idle");
		}
		
		override protected function onRenderTick(event:Event = null):void {
			
			if (_isWalking)
				model.moveForward( -20);
			
			if (_rotationDir != 0)
				model.rotationY += _rotationDir;
			
			super.onRenderTick(event);
			
			tf.htmlText = '.isPlaying = ' + model.isPlaying + '<br>.currentFrame = '+ model.currentFrame;
		}
		

		
	}
}